package com.example.news_demo.controller;

import com.example.news_demo.entity.Article;
import com.example.news_demo.entity.User;
import com.example.news_demo.entity.View;
import com.example.news_demo.service.ArticleService;
import com.example.news_demo.service.ViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/view")
public class ViewController {

    @Autowired
    private ViewService viewService;
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/submit")
    public String submit(HttpServletRequest request , Model model){
        HttpSession session=request.getSession();//获取session并将userName存入session对象y
        if (session == null){
            return "login";
        }
        User user = (User)session.getAttribute("loginUser");
        long article_id = Integer.parseInt(request.getParameter("article_id"));
        int interest = Integer.parseInt(request.getParameter("interest"));
        View view = new View();
        view.setArticle_id(article_id);
        view.setInterest(interest);
        view.setUser_id(user.getId());
        viewService.insertView(view);
        List<Article> articleList = articleService.getInterest(user.getId());
        model.addAttribute("article",articleList);
        List<Article> randomList = articleService.getRandom(10);
        model.addAttribute("randomList",randomList);
        return "index";
    }
}
